package walace.stocktracer;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;


class EasyStockWidgetConfig {
	
	public boolean mUpdateEnable;
	public int update_period_second;
	public List<StockItem> mItemsList;
	
	EasyStockWidgetConfig()
	{
		mUpdateEnable = false;
		update_period_second = 3;
		mItemsList = new ArrayList<StockItem>();
	}

};

public class StockWidgetConfigure extends Activity {
	
	public static final String ACTION_CONFIG_DONE = "walace.stocktracer.action.APPWIDGET_CONFIG_DONE";
	
	public static final String ITEM_WIDGET_ID = "item_widget_id";
	public static final String ITEM_SYMB = "item_sym";
	public static final String ITEM_NAME = "item_name";
	public static final String WIDGET_ENABLE = "widget_enable";
	
	private static final String TAG = "StockWidgetConfigure";
	
	public static List<StockItem> mItemsList;
	
	private EasyStockWidgetConfig mConfig;
	private Button eButtonConfigOk;
	private EditText mEditTextSymb;
	private CheckBox mCheckBoxEnable;
	private StockItem mCurrItem;
	private Spinner mSpinnerMarket;
	private String[] mStrMarket;
	private static final String[] mStrSym = {"TW","US","SS","SZ","HK","SI","AX","TO"};  
	
	int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		Log.d(TAG, "onCreate() start");
		//setResult(RESULT_CANCELED);
		setContentView(R.layout.widget_configure);
		eButtonConfigOk = (Button)findViewById(R.id.buttonConfigOk);
		mEditTextSymb = (EditText)findViewById(R.id.editTextSym);
		mCheckBoxEnable = (CheckBox)findViewById(R.id.checkBoxEnable);
		mSpinnerMarket = (Spinner) findViewById(R.id.spinner_market);
		
		mSpinnerMarket.setPrompt(this.getResources().getText(R.string.spinner_title));
		
		mConfig = StockTracerWidgetProvider.mConfig;
		
		mItemsList = mConfig.mItemsList;

		eButtonConfigOk.setOnClickListener(configOkButtonOnClickListener);	
		
		/*
		 * 上海市场末尾加.ss，深圳市场末尾加.sz
                              台北股市(.TW), 美国股市(.US), 大陆股市(.SS,.SZ), 
                              香港股市(.HK), 新加坡股市(.SI), 澳洲股市(.AX), 加拿大股市(.TO).
        */
		
		mStrMarket = new String[8];
		mStrMarket[0] = (String) this.getResources().getText(R.string.taiwan);	 		
		mStrMarket[1] = (String) this.getResources().getText(R.string.america);
		mStrMarket[2] = (String) this.getResources().getText(R.string.shanghai);
		mStrMarket[3] = (String) this.getResources().getText(R.string.shenzhen);
		mStrMarket[4] = (String) this.getResources().getText(R.string.hongkong);
		mStrMarket[5] = (String) this.getResources().getText(R.string.singapore);
		mStrMarket[6] = (String) this.getResources().getText(R.string.austrilia);
		mStrMarket[7] = (String) this.getResources().getText(R.string.canada);

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,mStrMarket);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpinnerMarket.setAdapter(adapter);

	    class SpinnerSelectedListener implements OnItemSelectedListener{  
	  
	        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {   
	            Log.d(TAG, "Spinner Selected items=" + mStrMarket[arg2]);
	        }  
	  
	        public void onNothingSelected(AdapterView<?> arg0) {  
	        }  
	    } 
	     	    
		mSpinnerMarket.setOnItemSelectedListener(new SpinnerSelectedListener());  
		mSpinnerMarket.setVisibility(View.VISIBLE);
		
		
		mCheckBoxEnable.setChecked(mConfig.mUpdateEnable);
		
	    Intent intent = getIntent();
	    Bundle extras = intent.getExtras();
	    if (extras != null) 
	    {	    	
	    	mAppWidgetId = extras.getInt(ITEM_WIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);	    		
	    	mCurrItem = StockWidgetItem.FindItem(mItemsList, mAppWidgetId);
		    	
	    	String[] symbs;
	    	symbs = mCurrItem.sym.split("\\.");

	    	if(symbs != null)
	    	{
		    	mEditTextSymb.setText(symbs[0]);
		    	int index = 0;
		    	for(index=0; index < mStrSym.length ;index++)
		    	{
		    		if(mStrSym[index].equals(symbs[1]))
		    		{
		    			break;
		    		}	    		
		    	}
		    	if(index >= mStrSym.length)
		    		index = 0;
		    	mSpinnerMarket.setSelection(index);
	    	}
	    }
	    // If they gave us an intent without the widget id, just bail.
	    if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) 
	    {
	    	finish();
	    }
	    Log.d(TAG, "onCreate() end");
	}
	
	private Button.OnClickListener configOkButtonOnClickListener = new Button.OnClickListener()
	{
		@Override
		public void onClick(View arg0) 
		{
			// TODO Auto-generated method stub
			 Log.d(TAG, "onClick() start");
			 
			 Intent ret_int = new Intent(ACTION_CONFIG_DONE);
			 String newsym = mEditTextSymb.getText().toString() + "." + mStrSym[mSpinnerMarket.getSelectedItemPosition()];
			 ret_int.putExtra(ITEM_WIDGET_ID, mAppWidgetId);
			 ret_int.putExtra(ITEM_SYMB, newsym);
			 ret_int.putExtra(WIDGET_ENABLE, mCheckBoxEnable.isChecked());
			 sendBroadcast(ret_int);
			 finish();
			 Log.d(TAG, "onClick() end");
        }
	};
};
